package ru.ognick.core;

import ru.ognick.logic.Praxis;
import android.os.SystemClock;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class BundleProcess {
	final private Praxis praxis;
	final private LinearLayout elements;
	final private int index;
	
	private int progress = 0;
	
	public BundleProcess(int index, Praxis praxis, LinearLayout box) {
		this.index = index;
		this.praxis = praxis;
		this.elements = box;
		
		progress = 0;
	}
	
	public boolean incProgress(){
		Chronometer chronometer = (Chronometer) elements.findViewWithTag("CHRONOMETR");
		if (praxis.getTimeSpan() == 0) {
			progress = (int) ((SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000);
			return true;
		}
		if (progress < praxis.getTimeSpan()){
			progress = (int) ((SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000);
			ProgressBar progressBar = (ProgressBar) elements.findViewWithTag("PROCESS_BAR");
			progressBar.setProgress(progress);
			return progress < praxis.getTimeSpan();
		} 
		else 
			return false;
	}
	
	public int getPeriodSpeak() {
		if ( praxis.getPeriodSignal() > 0 && progress % praxis.getPeriodSignal() == 0)
			return progress / praxis.getPeriodSignal();
		else 
			return 0;
	}
	
	public void resetProgress(){
		progress = 0; 
		ProgressBar progressBar = (ProgressBar) elements.findViewWithTag("PROCESS_BAR");
		progressBar.setProgress(progress);
	}
	
	public Praxis getPraxis() {
		return praxis;
	}
	public LinearLayout getElements() {
		return elements;
	}
	public int getIndex() {
		return index;
	}
}
